package com.cloud.common.core.handler;

import com.cloud.common.core.base.ApiResponse;
import com.cloud.common.core.base.ApiResponseEnum;
import com.cloud.common.core.util.ResponseUtil;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description: 处理401类型异常
 * @Auther: zhangxun
 * @Date: 2020/8/11 17:39
 */
// common并不是一个Spring Boot项目，所以即使在这两个类上使用@Component注解标
// 注，它们也不能被成功注册到各个微服务子系统的Spring IOC容器中。我们
// 可以使用@Enable模块驱动的方式来解决这个问题。在common模块的common路径下新建
// configure包，然后在该包下新建FebsAuthExceptionConfigure配置类

@Component //使用@Component注解标注，它们也不能被成功注册到各个微服务子系统的Spring IOC容器中
public class MyAuthExceptionEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException {
        ResponseUtil.makeResponse(
                response,
                MediaType.APPLICATION_JSON_UTF8_VALUE,
                HttpServletResponse.SC_UNAUTHORIZED,
                ApiResponse.respond(ApiResponseEnum.TICKET_EMPTY)
        );
    }
}